<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	
	/**
	 * @var Zend_Log
	 */
	protected $_logger;
	
	/**
	 * @var Zend_Application_Module_Autoloader
	 */
	protected $_resourceLoader;
	
	/**
	 * @var Zend_Controller_Front
	 */
	public $frontController; 
	
	/**
	*
	*ini config
	*/
	protected function _initConfig() 
	{
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini', $this->getEnvironment());
		 
		Zend_Registry::set('config', $config);
	}
	
	
	
	
	
    protected function _initLogging() {
    	
    	$logger = new Zend_Log ();
    
    	$writer = new Zend_Log_Writer_Firebug ();
    	$logger->addWriter ( $writer );
    
    	if ('production' == $this->getEnvironment ()) {
    		$filter = new Zend_Log_Filter_Priority ( Zend_Log::DEBUG );
    		$logger->addFilter ( $filter );
    	}
    
    	$this->_logger = $logger;
    	Zend_Registry::set ( 'log', $logger );
    }
	protected function _initDefaultController() {
		$config['modules'] = CFF::getConfig('modules');		 
		$this->setOptions($config);
		$this->bootstrap ( 'frontController' );
		$frontController =  $this->getResource('frontController');
		$frontController->setParam('noViewRenderer', true);
		 
	}
		
    
    protected function _initDefaultModuleAutoloader() {
    
    	$this->_logger->info ( 'Bootstrap ' . __METHOD__ );
		$modules = CFF::getConfig('modules');
		foreach($modules as $key=>$val){
			$this->_resourceLoader = new Zend_Application_Module_Autoloader ( array ('namespace' => ucfirst($key), 'basePath' => APPLICATION_PATH . $val ) );
		} 
		//var_dump($this->getResourceLoader());
    }
     
    
   /**
	*ini layout
	*
	*/
	public function _initLayout()
    {
		$layout = new Zend_Layout();
		$layout->setLayoutPath(APPLICATION_PATH . '/layouts');
		Zend_Registry::set('layout', $layout);
	 
		
    }
	
	public function _initViews()
    {
		 
		$config = Zend_Registry::get('config');
		$view = new Zend_View_Smarty($config->smartyConfig->template_dir, $config->smartyConfig->config);
     	 
		Zend_Registry::set('CffView', $view);
     }
	
	 public function _initDb()
    {
		$resource = $this->getPluginResource('db');
		$db = $resource->getDbAdapter();
		Zend_Db_Table_Abstract::setDefaultAdapter($db);
		Zend_Registry::set("db", $db); 	
    }	
    
    public function _initLang(){
        //语言设置 start
        if(isset($_GET['language'])){
            $language = $_GET['language'];
            setcookie('language',$language,time()+6400,'/');            
        }else{
            $language = isset($_COOKIE['language'])?$_COOKIE['language']:'en';            
        }
        // 		 echo $language;
        if(!in_array($language,array('en','cn'))){
            $language = 'en';
        }
        $langFile = APPLICATION_PATH."/languages/{$language}.php";
//         echo $langFile;exit;
        if(!file_exists($langFile)){
            die('language file not exists');
        }
        $lang = require_once $langFile;
        Zend_Registry::set('lang', $lang)	;
    }
}



/**
*
*CFF Class static functions
*
*/


class Cff 
{	
	/**
	 * getConfig 
	 */
	
	public static function getConfig($key)
	{
		return Zend_Registry::get('config')->get($key);
	} 
	
	
	
	/**
	*
	*rend smarty tpl
	*/
	public static function renderTpl($tpl, $layoutTemplate = null, $title = null, $scripts = null, $meta = null)
	{
		$layout = Zend_Registry::get('layout');
		
		if(null != $layoutTemplate && '' != $layoutTemplate){
			$layout->setLayout($layoutTemplate);
		}
		if(is_array($scripts)){
			foreach($scripts as $script){
				$layout->getView()->headScript()->appendFile(self::getConfig('baseUrl') . $script);
			}
		}

		$title = null == $title?self::getConfig('webTitle'):$title;
		$layout->getView()->headTitle($title);

		if($meta){
			if(isset($meta['keyword'])) {
				$layout->getView()->headMeta()->appendName('keywords',trim($meta['keyword']));
			}
			if(isset($meta['description'])) {
				$layout->getView()->headMeta()->appendName('description',trim($meta['description']));
			}
		}

     	$layout->content = Zend_Registry::get('CffView')->render($tpl);
		 
     	return $layout->render();
	}
	public static function setupMail() {
		try{
			$mail = Zend_Registry::get('mail');
			if($mail instanceof Zend_Mail) return $mail;
		}catch (Exception $e) {
		
			$config=Zend_Registry::get('config');
			$mailConfig = array('auth'=>$config->mails->config->auth,
							  'username'=>$config->mails->config->username,
							  'password'=>$config->mails->config->password,
							  'port' =>$config->mails->config->port,
	//						  'ssl' => $config->mails->config->ssl
							);
			$transport = new Zend_Mail_Transport_Smtp($config->mails->server, $mailConfig);
			Zend_Mail::setDefaultTransport($transport);
			$mail = new Zend_Mail('utf-8');
			$mail->setFrom($config->mails->from,'System Mail');
			Zend_Registry::set('mail', $mail);
			return $mail;
		}
     }
}	